package uodm.client;

import uodm.shared.model.OfficeData;
import uodm.shared.rpc.UodmService;
import uodm.shared.rpc.UodmServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class AddOfficeEntry implements EntryPoint {

	UodmServiceAsync uodmService = GWT.create(UodmService.class);

	private AddOfficeWidget mainWidget;

	@Override
	public void onModuleLoad() {
		mainWidget = new AddOfficeWidget(this);
		RootPanel.get("main").add(mainWidget);
	}

	public void on_submitButton_clicked() {
		final OfficeData odata = new OfficeData();
		odata.setName(mainWidget.officeNameBox.getText());
		odata.setNote(mainWidget.officeNoteBox.getText());
		uodmService.addOffice(odata, new AsyncCallback<Void>() {
			@Override
			public void onSuccess(Void result) {
				mainWidget.messageLabel.setText("Successful " + odata.getName());
			}

			@Override
			public void onFailure(Throwable caught) {
				mainWidget.messageLabel.setText("Failed: "
						+ caught.getMessage());
			}
		});
	}

	public void redirectToList() {
		Window.Location.assign("/listoffices.jsp");
	}

}
